import time
from common.Utils import SHPDriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#测试商城系统商品详情页测试
class ProductPage:
    def __init__(self):
        self.url = "http://49.235.61.184/"
        self.driver = SHPDriver.driver
        self.driver.get(self.url)
        self.wait = WebDriverWait(self.driver, 15)  # 全局显式等待（适配详情页加载慢）

    def productpageTest1(self):
        # 1.登录（添加成功判断，避免未登录白跑）
        login_success = SHPDriver.Auto_Login()

        # 2. 标记原窗口句柄（关键：记录初始窗口的“身份证”）
        original_handle = self.driver.current_window_handle
        print(f"原窗口句柄：{original_handle}")

        # 3.点击Apple iPhone 14 Pro Max
        self.driver.find_element(By.XPATH, '//*[@id="floor1"]/div[2]/div[2]/div/ul/li[2]/div/a/img').click()
        print("已点击商品，等待新窗口打开...")

        # 4. 核心：进入新窗口
        # 等待新窗口出现（动态等待，窗口数量>1即为打开）
        self.wait.until(lambda d: len(d.window_handles) > 1)
        # 遍历所有窗口，找到“非原窗口”的新窗口
        for handle in self.driver.window_handles:
            if handle != original_handle:
                self.driver.switch_to.window(handle)  # 切换到新窗口
                print(f"已进入新窗口，句柄：{handle}，当前URL：{self.driver.current_url}")
                break

         #5.加入购物车按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                                "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-secondary.cart-submit.buy-event.login-event"))
            )
        # 立即购买按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                                "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-primary.buy-submit.buy-event.login-event"))
            )

        # 收藏按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#clearcontent > div.photo-bottom.am-cf > span > em"))
            )
        # 详情按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                                "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.introduce.am-container.am-radius.am-margin-top-main > div.introduce-main.no-left-goods > div > div.am-sticky-placeholder > ul > li.am-active.goods-middle-nav-detail > a"))
            )
            # 评价按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                                "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.introduce.am-container.am-radius.am-margin-top-main > div.introduce-main.no-left-goods > div > div.am-sticky-placeholder > ul > li.goods-middle-nav-comments > a"))
        )
        # 截图（元素检查通过）
        SHPDriver.GetScreenShot("product_detail_check_elements")
        time.sleep(2)
        # 6. 购买商品（不提交订单）
        # 点击立即购买
        buy_now_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-primary.buy-submit.buy-event.login-event"))
        )
        buy_now_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("buy_now_btn")

        # 返回详情页
        self.driver.back()
        self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".buy-submit-container")))  # 确认回到详情页
        print("完成购买流程测试（未提交订单）")

        # 7. 通过二维码购买（点击展示二维码）
        qrcode_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.goods-qrcode > div > a"))
        )
        qrcode_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("product_detail_qrcode")
        print("完成二维码购买测试")

        # 8. 加入购物车（修改数量并断言）
        # 获取添加前购物车数量
        cart_count_before = self.wait.until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
                                             "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > strong"))
        ).text
        print(f"加入购物车前数量：{cart_count_before}")

        # 点击加入购物车
        add_cart_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-secondary.cart-submit.buy-event.login-event"))
        )
        add_cart_btn.click()
        time.sleep(2)  # 等待数量弹窗加载

        # 输入数量3
        quantity_input = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.theme-options.am-flex.am-flex-row.am-flex-items-center > dd > div > input"))
        )
        quantity_input.clear()
        quantity_input.send_keys("3")

        # 获取添加后数量并断言
        cart_count_after = self.wait.until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
                                             "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div"))
        ).text
        print(f"加入购物车后数量：{cart_count_after}")
        assert cart_count_after != cart_count_before, "购物车数量未变化，添加失败！"
        print("完成加入购物车测试")

        # 9. 收藏商品
        self.driver.back()
        collect_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#clearcontent > div.photo-bottom.am-cf > span > em"))
        )
        collect_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("product_detail_collect")
        print("完成收藏商品测试")

        # 10. 查看详情和评价
        # 查看详情（刷新当前标签）
        detail_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.introduce.am-container.am-radius.am-margin-top-main > div.introduce-main.no-left-goods > div > div.am-sticky-placeholder > ul > li.am-active.goods-middle-nav-detail > a"))
        )
        detail_btn.click()
        time.sleep(1)

        # 查看评价
        comment_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.introduce.am-container.am-radius.am-margin-top-main > div.introduce-main.no-left-goods > div > div.am-sticky-placeholder > ul > li.goods-middle-nav-comments > a"))
        )
        comment_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("product_detail_comments")
        print("完成详情和评价查看测试")

        # 11. 关闭当前窗口，切回原窗口
        self.driver.close()
        self.driver.switch_to.window(original_handle)
        print(f"已关闭详情页，切回原窗口：{original_handle}")

        # 12. 查看收藏列表
        # 点击收藏按钮
        favor_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a > span"))
        )
        favor_btn.click()

        # 进入商品收藏列表
        product_favor_link = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > ul > li > a"))
        )
        product_favor_link.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("product_favor_list")
        print("完成收藏列表查看测试")



# 执行测试
if __name__ == "__main__":
    product_page = ProductPage()
    product_page.productpageTest1()
    # 执行完成后保留浏览器5秒再关闭
    time.sleep(5)
    SHPDriver.quit_driver()
